const Base = require('./base')
module.exports = class extends Base {
  __before (...args) {
    this._modelName = this._modelName || this.ctx.controller
    this.resource = this._modelName
    this.modelInstance = yy.model(this._modelName)
    const action = this.ctx.action.toLowerCase()
    if (action === 'index') return
    const allMethods = this.allowMethod()
    if (allMethods.indexOf(action) > -1 && this[`${action}Action`]) {
      return this[`${action}Action`](...args)
    } else {
      return this.fail(-1, '不被允许的操作!', {})
    }
  }
  setWhere (map) {
    this.where = {}
    for (const i in map) {
      if (map[i]) this.where[i] = map[i]
    }
  }
  allowMethod () {
    return ['get', 'post', 'put', 'delete', 'forbid', 'revert', 'clear']
  }
  setModel () {
    this._modelName = this._modelName || this.ctx.controller
    this.resource = this._modelName
    this.modelInstance = yy.model(this._modelName)
  }
  indexAction (...args) {
    const method = this.ctx.method.toLowerCase()
    return this[`${method}Action`](...args)
  }
  async getAction () {
    const {order, page, pageSize, first, last, id} = this.get()
    this.where = this.where || {}
    if (id && !page) {
      this.where._id = id
      const d = await this.modelInstance.where(this.where).find()
      return this.success(d)
    } else if (first || last) {
      const d = await this.modelInstance.where(this.where).order(order).cursor(first, last, pageSize)
      return this.success(d)
    } else {
      const list = await this.modelInstance.where(this.where).order(order).page(page, pageSize).select()
      const count = await this.modelInstance.where(this.where).count()
      return this.success({list, count, page, pageSize})
    }
  }
  /**
   * put resource
   * @return {Promise} []
   */
  async postAction () {
    const data = this.post()
    if (think.isEmpty(data)) {
      return this.fail('提交数据为空')
    }
    const d = await this.modelInstance.add(data)
    return this.success({ id: d._id })
  }
  /**
   * delete resource
   * @return {Promise} []
   */
  async deleteAction () {
    const data = this.post()
    this.where = {}
    data.id = data.id || data._id
    if (data.id) {
      this.where._id = data.id
      delete data.id
    } else if (Object.keys(this.where).length === 0) {
      return this.fail(-1, '找不到相应数据')
    }
    if (Object.keys(this.where).length === 0) {
      return this.fail(-2, '删除条件不能为空')
    }
    const rows = await this.modelInstance.where(this.where).remove()
    return this.success({ affectedRows: rows })
  }
  /**
   * update resource
   * @return {Promise} []
   */
  async putAction () {
    const data = this.post()
    this.where = this.where || {}
    data.id = data.id || data._id
    if (data.id) {
      this.where._id = data.id
      delete data.id
    } else if (Object.keys(this.where).length === 0) {
      return this.fail('找不到相应数据')
    }
    if (think.isEmpty(data)) {
      return this.fail('提交数据为空')
    }
    const rows = await this.modelInstance.where(this.where).update(data)
    return this.success({ affectedRows: rows })
  }
  async forbidAction () {
    this.setModel()
    const {id} = this.post()
    if (!id) {
      return this.fail('找不到相应数据')
    }
    const rows = await this.modelInstance.where({_id: id}).update({status: 1})
    return this.success({ affectedRows: rows })
  }
  async revertAction () {
    this.setModel()
    const {id} = this.post()
    if (!id) {
      return this.fail('找不到相应数据')
    }
    const rows = await this.modelInstance.where({_id: id}).update({status: 0})
    return this.success({ affectedRows: rows })
  }
  async clearAction () {
    this.setModel()
    if (think.env === 'testing' || think.env === 'development') {
      const rows = await this.modelInstance.where({}).remove()
      return this.success({ affectedRows: rows })
    } else {
      return this.fail(-1, '正式环境不具备清空功能')
    }
  }
}
